package cn.pugle.base.lambda;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by tzp on 16-10-24.
 */
public class StreamTest {
    public static void basic() {
        List<Long> entityIds = new ArrayList<Long>() {{
            add(1L);
            add(3L);
            add(2L);
            add(null);
        }};

        //arr = Arrays.stream(arr).filter(Objects::nonNull).toArray(ListNode[]::new);

        String collect = entityIds.stream().filter(Objects::nonNull).sorted().distinct().map(Object::toString).collect(Collectors.joining(","));
        System.out.println(collect);
    }

    //来自LC406
    public static void oSort(int[][] people) {
        int[][] peopleSorted;
        peopleSorted = Arrays.stream(people)
                .sorted(new Comparator<int[]>() {
                    @Override
                    public int compare(int[] o1, int[] o2) {
                        int r = Integer.compare(o1[0], o2[0]);
                        return r != 0 ? r : Integer.compare(o1[1], o2[1]);
                    }
                })
                .toArray(int[][]::new);

        peopleSorted = Arrays.stream(people)
                .sorted((o1, o2) -> {
                    int r = Integer.compare(o1[0], o2[0]);
                    return r != 0 ? r : Integer.compare(o1[1], o2[1]);
                })
                .toArray(int[][]::new);

        peopleSorted = Arrays.stream(people)
                .sorted(Comparator.comparingInt((int[] o) -> o[0])
                        .thenComparingInt(o -> o[1]))
                .toArray(int[][]::new);//秀啊

        //.thenComparing((o1, o2) -> - Integer.compare(o1[1], o2[1]))

//        LinkedList<int[]> list = new LinkedList<>();
//        for (int i = 0; i < peopleSorted.length; i++) {
//            list.add(peopleSorted[i][1], peopleSorted[i]);
//        }
//        return list.toArray(new int[0][]);//这个几把参数
    }

    public static void main(String[] args) {
        testFlatMap();
    }

    public static void testFlatMap() {
        //counts.compute(c, (oldValue, newValue) -> oldValue == null ? 1 : oldValue + 1);

        /**获取单词，并且去重**/
        List<String> list = Arrays.asList("hello welcome", "world hello", "hello world",
                "hello world welcome");

        //map和flatmap的区别
        list.stream()
                .map(item -> Arrays.asList(item.split(" ")))
                .distinct()
                .collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("---------- ");
        list.stream()
                .flatMap(item -> Arrays.stream(item.split(" ")))
                .distinct()
                .collect(Collectors.toList()).forEach(System.out::println);

        //实际上返回的list是不同的
        List<Stream<String>> listResult = list.stream().map(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());
        List<String> listResult2 = list.stream().flatMap(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());

        System.out.println("---------- ");

        //也可以这样
        list.stream().map(item -> item.split(" ")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()).forEach(System.out::println);

        System.out.println("================================================");

        /**相互组合**/
        List<String> list2 = Arrays.asList("hello", "hi", "你好");
        List<String> list3 = Arrays.asList("zhangsan", "lisi", "wangwu", "zhaoliu");

        list2.stream().map(item -> list3.stream().map(item2 -> item + " " + item2)).collect(Collectors.toList()).forEach(System.out::println);
        list2.stream().flatMap(item -> list3.stream().map(item2 -> item + " " + item2)).collect(Collectors.toList()).forEach(System.out::println);

        List<Stream<String>> list2Result = list2.stream().map(item -> list3.stream().map(item2 -> item + " " + item2)).collect(Collectors.toList());
        List<String> list2Result2 = list2.stream().flatMap(item -> list3.stream().map(item2 -> item + " " + item2)).collect(Collectors.toList());


    }
}
